// BlogLikeDao.java
package net.web.dao;

import net.web.common.utils.JdbcUtils;
import net.web.entity.BlogLike;

import java.util.List;

public class BlogLikeDao {
    public boolean insert(BlogLike blogLike) {
        String sql = "INSERT INTO blog_like (blog_id, user_id) VALUES (?,?)";
        int rows = JdbcUtils.executeUpdate(sql, blogLike.getBlogId(), blogLike.getUserId());
        return rows > 0;
    }

    public boolean delete(int blogId, int userId) {
        String sql = "DELETE FROM blog_like WHERE blog_id = ? AND user_id = ?";
        return JdbcUtils.executeUpdate(sql, blogId, userId) > 0;
    }

    public boolean isLiked(int blogId, int userId) {
        String sql = "SELECT COUNT(*) FROM blog_like WHERE blog_id = ? AND user_id = ?";
        long count = JdbcUtils.executeQueryOneValue(sql, blogId, userId);
        return count > 0;
    }

    public int getLikeCount(int blogId) {
        String sql = "SELECT COUNT(*) FROM blog_like WHERE blog_id = ?";
        return (int) JdbcUtils.executeQueryOneValue(sql, blogId);
    }

    public boolean updateLikeCount(int blogId, int delta) {
        String sql = "UPDATE blog SET likecount = GREATEST(0, likecount + ?) WHERE id = ?";
        return JdbcUtils.executeUpdate(sql, delta, blogId) > 0;
    }
}
